namespace Digitalmes.WebApi.Infrastructure;

/// <summary>
/// Endpoint 路由构造器扩展。
/// </summary>
public static class IEndpointRouteBuilderExtensions
{
    /// <summary>
    /// GET 请求。
    /// </summary>
    /// <param name="builder"></param>
    /// <param name="handler">匹配终结点时执行的委托。</param>
    /// <param name="pattern">路由模式。</param>
    /// <returns></returns>
    public static IEndpointRouteBuilder MapGet(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
    {
        Guard.Against.AnonymousMethod(handler);

        builder.MapGet(pattern, handler)
            .WithName(handler.Method.Name);

        return builder;
    }

    /// <summary>
    /// POST 请求。
    /// </summary>
    /// <param name="builder"></param>
    /// <param name="handler">匹配终结点时执行的委托。</param>
    /// <param name="pattern">路由模式。</param>
    /// <returns></returns>
    public static IEndpointRouteBuilder MapPost(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
    {
        Guard.Against.AnonymousMethod(handler);

        builder.MapPost(pattern, handler)
            .WithName(handler.Method.Name);

        return builder;
    }

    /// <summary>
    /// PUT 请求。
    /// </summary>
    /// <param name="builder"></param>
    /// <param name="handler">匹配终结点时执行的委托。</param>
    /// <param name="pattern">路由模式。</param>
    /// <returns></returns>
    public static IEndpointRouteBuilder MapPut(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
    {
        Guard.Against.AnonymousMethod(handler);

        builder.MapPut(pattern, handler)
            .WithName(handler.Method.Name);

        return builder;
    }

    /// <summary>
    /// DELETE 请求。
    /// </summary>
    /// <param name="builder"></param>
    /// <param name="handler">匹配终结点时执行的委托。</param>
    /// <param name="pattern">路由模式。</param>
    /// <returns></returns>
    public static IEndpointRouteBuilder MapDelete(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
    {
        Guard.Against.AnonymousMethod(handler);

        builder.MapDelete(pattern, handler)
            .WithName(handler.Method.Name);

        return builder;
    }
}
